/**
 * 商家导航
 * @author Qasim <15750783791@163.com>
 * @version 2016/9/28
 */
var index = {};
document.addEventListener('plusready' , ready ,  false);

function ready(){
	//加载数据
   	info();
	
	//搜索
 	$('.search').on('click' , function(){
 		search(33); 
 	})
}

function info(){
	plus.nativeUI.showWaiting('加载中...');
	$.ajax({
			
		type:"post",
		
		url:apiRoot +'/Home/Business/business/',
		
		dataType : 'json', 
		
		success : function(data){
			plus.nativeUI.closeWaiting(); 
			if(data.wuxing != ''){
				index[0] = 2; 
				var html = '';
				$.each(data.wuxing, function(k , v) {
					html += '<div class="table-view-cell table-view background-w sj bar-buttom list0 lists" data-number="'+v.order_number+'" data-good="'+v.good_number+'" data-id = "'+v.id+'" data-tid = "'+v.tid+'" data-level="'+v.level+'" data-scale ="'+v.scale+'" data-addr ="'+v.address+'" data-details="'+v.details+'" data-contacts="'+v.contacts+'" data-phone="'+v.phone+'">';
					html += '<li><span class="title">'+v.title+'</span></li>';  
					html += '<li class="color-ddd line-h-25"><span class="nav-wjx">'; 
					html += '<span class="iconfont icon-xingxing color-zx-2"></span>'; 
					html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 1 ? "" : "color-c")+'"></span>'; 
					html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 2 ? "" : "color-c")+'"></span>'; 
					html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 3 ? "" : "color-c")+'"></span>'; 
					html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 4 ? "" : "color-c")+'"></span>';
					html += '<span class="font-12">&emsp;'+v.number+'评价</span></li><li><span class="color-c font-12 classify">'+v.addtime+'</span></li></div>';
				});
				$('#item1mobile .mui-scroll').html(html);
			}
			 
			if(data.hot != ''){
				index[1] = 2;
				var html = '';
				$.each(data.hot, function(k , v) {
					html += '<div class="table-view-cell table-view background-w sj bar-buttom list1 lists" data-number="'+v.order_number+'" data-good="'+v.good_number+'" data-id = "'+v.id+'" data-tid = "'+v.tid+'" data-level="'+v.level+'" data-scale ="'+v.scale+'" data-addr ="'+v.address+'" data-details="'+v.details+'" data-contacts="'+v.contacts+'" data-phone="'+v.phone+'">';
					html += '<li><span class="title">'+v.title+'</span></li>';  
					html += '<li class="color-ddd line-h-25"><span class="nav-wjx">'; 
					html += '<span class="iconfont icon-xingxing color-zx-2"></span>'; 
					html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 1 ? "" : "color-c")+'"></span>'; 
					html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 2 ? "" : "color-c")+'"></span>'; 
					html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 3 ? "" : "color-c")+'"></span>'; 
					html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 4 ? "" : "color-c")+'"></span>';
					html += '<span class="font-12">&emsp;'+v.number+'评价</span></li><li><span class="color-c font-12 classify">'+v.addtime+'</span></li></div>';
				});
				$('#item2mobile .mui-scroll').html(html);
			}
			if(data.collection != ''){
				index[2] = 2;
				var html = ''; 
				$.each(data.collection, function(k , v) {
					html += '<div class="table-view-cell table-view background-w sj bar-buttom list1 lists" data-number="'+v.order_number+'" data-good="'+v.good_number+'" data-id = "'+v.id+'" data-tid = "'+v.tid+'" data-level="'+v.level+'" data-scale ="'+v.scale+'" data-addr ="'+v.address+'" data-details="'+v.details+'" data-contacts="'+v.contacts+'" data-phone="'+v.phone+'">';
					html += '<li><span class="title">'+v.title+'</span></li>';  
					html += '<li class="color-ddd line-h-25"><span class="nav-wjx">'; 
					html += '<span class="iconfont icon-xingxing color-zx-2"></span>'; 
					html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 1 ? "" : "color-c")+'"></span>'; 
					html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 2 ? "" : "color-c")+'"></span>'; 
					html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 3 ? "" : "color-c")+'"></span>'; 
					html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 4 ? "" : "color-c")+'"></span>';
					html += '<span class="font-12">&emsp;'+v.number+'评价</span></li><li><span class="color-c font-12 classify">'+v.addtime+'</span></li></div>';
				});
				$('#item3mobile .mui-scroll').html(html);
			}
			details();
			
		},
		error : function(e){
			
			console.log(JSON.stringify(e));
			
			plus.nativeUI.closeWaiting();
			
			toast('连接超时,请检查网络');
			
		}
	});
	
}

function loadMore(subscript){  
	
	plus.nativeUI.showWaiting('加载中...');
	
//	console.log(apiRoot + '/Home/Business/loadMore/page/'+index[subscript] +'/subscript/'+subscript)
	$.ajax({  
		  
		type:"post",  
		
		url: apiRoot + '/Home/Business/loadMore/' ,  
		
		data:{ 
			page : index[subscript],
			subscript : subscript
		},  
		 
		dataType : 'json', 
		
		success : function(data){ 
			plus.nativeUI.closeWaiting();
			if(data != ''){  
				index[subscript]++;
//				console.log(subscript)
//				console.log(JSON.stringify(data));
				$.each(data , function(k , v) {
					var html = '<div class="table-view-cell table-view background-w sj bar-buttom list'+subscript+'  lists" data-number="'+v.order_number+'" data-good="'+v.good_number+'" data-id = "'+v.id+'" data-tid = "'+v.tid+'" data-level="'+v.level+'" data-scale ="'+v.scale+'" data-addr ="'+v.address+'" data-details="'+v.details+'" data-contacts="'+v.contacts+'" data-phone="'+v.phone+'">';
					html += '<li><span class="title">'+v.title+'</span></li>';
					html += '<li class="color-ddd line-h-25"><span class="nav-wjx">';  
					html += '<span class="iconfont icon-xingxing color-zx-2"></span>'; 
					html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 1 ? "" : "color-c")+'"></span>';  
					html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 2 ? "" : "color-c")+'"></span>'; 
					html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 3 ? "" : "color-c")+'"></span>'; 
					html += '<span class="iconfont icon-xingxing color-zx-2 '+(v.level > 4 ? "" : "color-c")+'"></span>';
					html += '<span class="font-12">&emsp;'+v.number+'评价</span></li><li><span class="color-c font-12 classify">'+v.addtime+'</span></li></div>';
					$('#item'+(parseInt(subscript) + 1 )+'mobile .mui-scroll').append(html); 
				});
				details();
			}else{
				toast('没有更多的数据了');
			}
				
		},  
		
		error : function(e){ 
			
			plus.nativeUI.closeWaiting();
			
			toast('连接超时,请检查网络'); 
			
			alert(JSON.stringify(e))
			
		} 
	});
 	
}


function details(){
	$('.lists').unbind();
	$('.lists').on('tap' , function(){
		plus.nativeUI.showWaiting('跳转中...');
		var nav_inf = plus.webview.create('nav-inf.html', 'bbs-inf', {}, {
			company_name : $(this).find('.title').text(),
			level : $(this).attr('data-level'),
			classify : $(this).find('.classify').text(),
			aid : $(this).attr('data-id'),
			tid : $(this).attr('data-tid'),
			body : $(this).attr('data-details'),
			scale : $(this).attr('data-scale'),
			address : $(this).attr('data-addr'),
			phone : $(this).attr('data-phone'),
			contacts : $(this).attr('data-contacts'),
			number :  $(this).attr('data-number'), 
			good :  $(this).attr('data-good')
		});
		
		nav_inf.addEventListener('loaded', function (){
			nav_inf.show('pop-in');
			plus.nativeUI.closeWaiting();
		})	
	}) 
}